package com.moss.cloud.common.security.component;

import com.moss.cloud.common.core.constant.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ApiTokenService {
    @Resource
    private RedisTemplateOps redisTemplateOps;

    /**
     * 校验幂等性token
     *
     * @param key
     * @return
     */
    public String findApiToken(String key, String params) {
        boolean hasKey = redisTemplateOps.hasKey(Constant.X_CLIENT_TOKEN + key);
        if (hasKey) {
            return (String) redisTemplateOps.get(Constant.X_CLIENT_TOKEN + key);
        } else {
            redisTemplateOps.set(Constant.X_CLIENT_TOKEN + key, params, Constant.X_CLIENT_API_TIME);
            return StringUtils.EMPTY;
        }
    }
}
